package com.lh.utils;

import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

public class Answer<T>{

    //200正常
    private int code = HttpServletResponse.SC_BAD_REQUEST;
    //对象
    private T result;
    //信息
    private String msg;
    //应答ID
    private String id = UUID.randomUUID().toString();

    public Answer() {
        super();
    }

    public Answer(T result) {
        super();
        this.result = result;
    }

    public int getCode() {
        return code;
    }

    public Answer<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public T getResult() {
        return result;
    }

    public Answer<T> setResult(T result) {
        this.result = result;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Answer<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Answer<T> set(T result, String msg) {
        this.setResult(result);
        this.setMsg(msg);
        return this;
    }

    public Answer<T> set(String msg, int code) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public Answer<T> set(int code, T result) {
        this.setCode(code);
        this.setResult(result);
        return this;
    }

    public Answer<T> set(int code, T result, String msg) {
        this.setCode(code);
        this.setResult(result);
        this.setMsg(msg);
        return this;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public static <T> Answer<T> build(int code, T data, String msg, String id) {
        Answer<T> result = Answer.build(code, data, msg);
        result.setId(id);
        return result;
    }

    public static <T> Answer<T> build(int code, T data, String msg) {
        Answer<T> result = new Answer<T>();
        result.setCode(code);
        result.setResult(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Answer<T> build() {
        return new Answer<T>();
    }

    public static <T> Answer<T> build(T data) {
        return new Answer<T>(data);
    }

    public static <T> Answer<T> error(BindingResult bindingResult) {
        return Answer.build(HttpServletResponse.SC_BAD_REQUEST, null,
                bindingResult.getFieldErrors().get(0).getDefaultMessage());
    }

    public static <T> Answer<T> error(String msg) {
        return Answer.build(HttpServletResponse.SC_BAD_REQUEST, null, msg);
    }

    public static <T> Answer<T> error(ResultOne<?> result) {
        return Answer.build(HttpServletResponse.SC_BAD_REQUEST, null, result.getMsg(), result.getId());
    }

    public static <T> Answer<T> point(ResultOne<T> result) {
        return Answer.build(result.isSuccess() ? HttpServletResponse.SC_OK : HttpServletResponse.SC_BAD_REQUEST,
                result.getData(), result.getMsg(), result.getId());
    }

    public static <T> Answer<T> ok(T data, String msg) {
        Answer<T> result = new Answer<T>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setResult(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Answer<T> ok(T data) {
        Answer<T> result = new Answer<T>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setResult(data);
        result.setMsg("请求成功");
        return result;
    }
}